package mark.tom.gear.vm

import android.app.Activity
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import mark.tom.gear.base.callback.CallBack
import mark.tom.gear.base.extension.send
import mark.tom.gear.base.model.Result
import mark.tom.gear.base.utils.GmuUtil

/**
 * @Description: 开屏页ViewModel
 * @Author: ttmk110@163.com
 * @Package: mark.tom.gear.vm
 * @Date: 2021/4/2
 * @Copyright: 版权归 Mark 所有
 * <ModifyLog>
 * @ModifyContent:
 * @Author:
 * @Date:
 * </ModifyLog>
 */
class SplashViewModel(application: Application) : AndroidViewModel(application) {

    // GMU Manager初始化结果LiveData
    val gmuManagerInitResultLiveData = MutableLiveData<Result>()
    // GMU Service初始化结果LiveData
    val gmuServiceInitResultLiveData = MutableLiveData<Result>()

    /**
     * Description: 初始化GMU Manager服务
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/4/2 14:14
     */
    fun initGMUManager() {
        // Disable API Dialog
        GmuUtil.closeAndroidPDialog()
        // 初始化GMUManager
        GmuUtil.initManager(mCallBack = object : CallBack<Any, Unit> {
            override fun onSuccess(param: Any) {
                gmuManagerInitResultLiveData.send(Result.build(true))
            }

            override fun onFail(param: Any) {
                gmuManagerInitResultLiveData.send(Result.build(false, "GMUManager Init Fail"))
            }
        })
    }

    /**
     * Description: 初始化GMU Service包服务
     * Copyright: Copyright (c) 2021
     * Project: Gear
     * Author: ttmk110@163.com
     * Date: 2021/4/2 15:52
     */
    fun initGMUService(mActivity: Activity) {
        // Service 初始化
        GmuUtil.initService(mActivity)
        // 下载离线包
        GmuUtil.updateOfflinePackage(mActivity)
        // 上传本地崩溃日志
        GmuUtil.sendStartInfo2Server(mActivity)
        // 通知UI层 初始化GMU Service成功
        gmuServiceInitResultLiveData.send(Result.build())
    }
}